//
//  HLNetWorkController.swift
//  SwiftPods
//
//  Created by hulinhu on 2018/1/16.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import Moya
import ObjectMapper
import Alamofire
import RxSwift
import MJRefresh
import Then
import RxGesture

let cellID = "HLHomeTableViewCell"


class HomeViewController: UIViewController {
    
    let viewModel = HLHomeViewModel()
    var bag : DisposeBag = DisposeBag()

    var tableView = UITableView.init(frame: CGRect.init(x: 0, y: kNavBarHeight, width: kScreenWidth, height: kScreenHeight-kNavBarHeight), style: .plain).then {
        $0.rowHeight = 80;
        $0.tableFooterView = UIView.init()
        $0.register(UINib.init(nibName: "HLHomeTableViewCell", bundle: nil), forCellReuseIdentifier: cellID)
        $0.backgroundColor = UIColor.yellow// 重新系统方法
    }
    
    // 只定义类型 不进行实例化
    var heardView2 :  HLHomeHeardView!
    
//    // 定义的时候进行 实例化
//    var heardView = HLHomeHeardView().then({ (heardView) in
//        heardView.frame = CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 44)
//    })
    
//    var heardView2 = HLHomeHeardView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 44)) { (index) in
//        print(index)
//
//    }
    
//    var heardView2 = HLHomeHeardView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 44)) { (index) in
//        print(index)
//        weak var weakself = self
//
//        weakself.jump()
//    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "首页"
        view.backgroundColor = UIColor.white
        setupUI()
        tableViewAction()
    }
    
    fileprivate  func setupUI() {
        
       heardView2 = HLHomeHeardView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 44)) { [weak self] (index) in
            print(index)
            self?.jump()
        }
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior(rawValue: 2)!
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        };
        
        // 返回block
//        heardView.itemClicked = { (index) in
//            NSLog(index)
//            self.jump()
//        }
        tableView.tableHeaderView = heardView2;
        
        
        // 添加 TableView
        view.addSubview(tableView)
        
        // 注意设置顺序 要不监听不走
        viewModel.tableView = tableView;
        
        // 设置数据绑定
        viewModel.setupConfig();

        weak var weakself = self
        tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            weakself?.viewModel.requestHomeDataCommond.onNext(true)
        })
        tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            weakself?.viewModel.requestHomeDataCommond.onNext(false)
        })
        
        tableView.mj_header.beginRefreshing()
        
    }
    
    fileprivate func tableViewAction() {
        
        tableView.rx
            .itemSelected
            .subscribe(onNext: {
                (index : IndexPath) in
                NSLog("\(index.row)")
            }).disposed(by: bag)
        
        tableView.rx
            .modelSelected(StoryModel.self)
            .subscribe(
                onNext:{
                    value in
                    NSLog(value.title)
            }).disposed(by: bag)
    }
   
}
extension HomeViewController {
    func jump() {
           self.navigationController?.pushViewController(RxSwiftViewController(), animated: true)
    }
    
}
